﻿// 功能：列表项集合
// 描述：
// 作者：温伟鹏
// 日期：2010/6/2 10:58:12

using System;
using System.Collections.Generic;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 列表项集合
    /// </summary>
    public class ListItemCollection : IEnumerable<ListItem>
    {
        private List<ListItem> list;
        private ListControlBase owner;

        /// <summary>
        /// 获取保存列表项的列表
        /// </summary>
        protected List<ListItem> List
        {
            get{
                return list;
            }
        }
        /// <summary>
        /// 获取用户当前选中的列表项数组
        /// </summary>
        public ListItem[] SelectedItems
        {
            get
            {
                List<ListItem> items = new List<ListItem>();

                lock (list)
                {
                    foreach (ListItem item in List)
                    {
                        if (item.Selected)
                        {
                            items.Add(item);
                        }
                    }
                }

                return items.ToArray();
            }
        }
        /// <summary>
        /// 获取列表项集合中的项目数
        /// </summary>
        public int Count
        {
            get
            {
                return list.Count;
            }
        }
        /// <summary>
        /// 构造列表项集合
        /// </summary>
        /// <param name="owner">列表项集合的所有者</param>
        internal ListItemCollection(ListControlBase owner)
        {
            this.owner = owner;
            list = new List<ListItem>();
        }
        /// <summary>
        /// 获取指定索引处的列表项
        /// </summary>
        /// <param name="index">列表项索引</param>
        /// <returns></returns>
        public ListItem this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                    return null;
                return list[index];
            }
        }
        /// <summary>
        /// 向列表中添加列表项
        /// </summary>
        /// <param name="item"></param>
        public void Add(ListItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            list.Add(item);
            owner.AddItem(item.Text);
        }
        /// <summary>
        /// 将指定的列表项集合添加到此集合中
        /// </summary>
        /// <param name="collection"></param>
        public void AddRange(IEnumerable<ListItem> collection)
        {
            list.AddRange(collection);

            foreach (ListItem item in collection)
            {
                owner.AddItem(item.Text);
            }
        }
        /// <summary>
        /// 移除指定的列表项
        /// </summary>
        /// <param name="item"></param>
        public void Remove(ListItem item)
        {
            if (item == null)
            {
                return;
            }

            for (int i = 0; i < list.Count; i++)
            {
                if (item == list[i])
                {
                    RemoveAt(i);
                    return;
                }
            }
        }
        /// <summary>
        /// 获取指定列表项在集合中的索引值
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int GetItemIndex(ListItem item)
        {
            if (item == null)
            { throw new ArgumentNullException("item"); }

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] == item)
                    return i;
            }

            return -1;
        }
        /// <summary>
        /// 根据指定索引移除列表项
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= list.Count)
                return;

            list.RemoveAt(index);
            owner.RemoveItem(index);
        }
        /// <summary>
        /// 清空列表项集合
        /// </summary>
        public void Clear()
        {
            list.Clear();
            owner.RemoveAllItems();
        }
        /// <summary>
        /// 设置集合中被选中的列表项
        /// </summary>
        /// <param name="index"></param>
        /// <param name="selected">是否选中</param>
        public virtual void SetItemSelected(int index,bool selected)
        {
            if (index >= list.Count)
                return;

            // 如果选中索引小于0，则将所有项设置为未选中
            if (index < 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].Selected = false;
                }

                return;
            }

            for (int i = 0; i < list.Count; i++)
            {
                if (i != index)
                {
                    list[i].Selected = !selected;
                }
                else
                {
                    list[i].Selected = selected;
                }
            }
        }

        #region IEnumerable<ListItem> 成员

        public IEnumerator<ListItem> GetEnumerator()
        {
            foreach (ListItem item in list)
            {
                yield return item;
            }
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return list.GetEnumerator();
        }

        #endregion
    }
}
